"""Anonycoin --- Bitcoin simulator with anonymity throught group signature

Copyright(c) 2016 Daniel Pimentel <d4n1@member.fsf.org>

This file is part Anonycoin.

Anonycoin is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.

Anonycoin is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Anonycoin.  If not, see <http://www.gnu.org/licenses/>.
"""

from datetime import datetime
import threading
from blockchain import Blockchain


class Miner(threading.Thread):
    """Miner Bitcoin simulator thread."""

    def __init__(self, block, transaction, ip):
        """Constructor.
        block - current block
        transaction - transactor to be validated
        ip - ip address
        timestamp - mining timestamp
        difficulty - difficulty level to validate the transaction
        """
        threading.Thread.__init__(self)
        self.block = block
        self.transaction = transaction
        self.ip = ip
        self.timestamp = datetime.now()
        self.difficulty = 0

    def pow(self):
        """Proof-of-Work Bitcoin simulator using fibonacci sequence."""
        fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)
        self.difficulty = fib(self.block)

    def mining(self, simulation):
        """Mining Bitcoin simulator.
        """
        blockchain = Blockchain(self.block, self.transaction, self.ip,
                                self.timestamp, self.difficulty)
        blockchain.blocking(simulation)
